//
//  StatusCell.swift
//  Weibo
//
//  Created by apple on 15/11/30.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

/*
顶部视图 + 配图视图 + 底部工具
*/


let StatusCellMargin: CGFloat = 12
let StatusCellImageWidth: CGFloat = 35

class StatusCell: UITableViewCell {
    
    // 底部约束属性
    var topConstraints: Constraint?
    
    //添加模型属性
    var status: Status? {
        didSet {
            //            print(status?.retweeted_status?.text)
            originalView.status = status
            
            //移除工具条的顶部约束
            topConstraints?.uninstall()
            //设置转发微博的数据模型
            if status?.retweeted_status != nil {
                //就是转发微博
                retweetedView.retweetedStatus = status?.retweeted_status
                //显示转发微博
                retweetedView.hidden = false
                //更新约束
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
            } else {
                //隐藏转发微博视图
                retweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.topConstraints = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
        }
    }
    
    //MARK: 重写初始化方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = .None
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置UI
    private func setupUI() {
        contentView.addSubview(originalView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(bottomView)
        
        //设置布局
        originalView.snp_makeConstraints { (make) -> Void in
            //            make.top.equalTo(contentView.snp_top)
            //            make.left.equalTo(contentView.snp_left)
            //            make.right.equalTo(contentView.snp_right)
            make.top.left.right.equalTo(contentView)
        }

        
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
        }
        
        //底部视图的约束
        bottomView.snp_makeConstraints { (make) -> Void in
            self.topConstraints = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(40)
        }
        
        //需要给contentView添加约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            //底部的约束非常关键
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }
    
    //MARK: 懒加载子视图
    //原创微博
    private lazy var originalView: StatusOriginalView = StatusOriginalView()
    private lazy var retweetedView: StatusRetweetedView  = StatusRetweetedView()
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
   

}
